#!/bin/bash
# smazeme obrazovku
clear

# cistime adresare?
if [ "$1" == "clean" ]; then
  echo Cistim...
  rm -rf _tools/_compile/logs/*
  rm -rf tmp/*
  rm -rf output/files/*
  exit
fi

# spoustime v qemu?
if [ "$1" == "qemu" ]; then
  echo Spoustim QEMU, emuluji...
  qemu -fda output/AltairOS.img
  exit
fi

# zjistime posledni parametr
for arg in $@
do
  false
done

# pokud jdeme provadet akci, ktera bude potrebovat floppy image
if [ "$arg" == "-i" ] || [ "$1" == "-a" ] || [ "$1" == "" ]; then
  echo Vytvarim IMG soubor...
  rm -f output/AltairOS.img
  cp output/AltairOS-empty.img output/AltairOS.img
  sudo mkdir tmp/mnt --mode=777
  sudo mount output/AltairOS.img tmp/mnt -v -o rw,loop,uid=$UID
  echo ----------------------------------------------
fi

# vytvoreni pomocneho _c.bat souboru
echo Kompiluji...
echo "compile.exe $1 $2" > _c.bat

# jestli je jako 3ti paramer uveden "-i", provede se dodatecne pridani do floppy image
if [ "$arg" = "-i" ] && [ ! "$1" = "-i" ]; then
  echo "compile.exe -i" >> _c.bat
fi

#if [ ! "$1" = "-c" ] && [ ! "$1" = "" ] && [ ! "$1" = "-a" ]; then
  echo exitemu >> _c.bat
#fi

# jestli kompiluji kompletni OS, je dobre promazat vsechny slozky a soubory
if [ "$1" = "" ] || [ "$1" = "-a" ]; then
  echo Vymazavam slozku OUTPUT/FILES a LOGY
  rm -rf _tools/_compile/logs/*
  rm -rf output/files/*
fi

# spustime dosemu, ve kterem probehne kompilace
dosemu D+M -E _c.bat
rm _c.bat

# zkopirujeme defaultni hierarchii
cp -fr output/files-default/* output/files
cp -fr output/files-default/* tmp/mnt

# jestli jsme pouzivali floppy image, odpojime ho
if [ "$arg" == "-i" ] || [ "$1" == "-a" ] || [ "$1" == "" ]; then
  sudo umount tmp/mnt
fi
chmod -R 777 *
chmod 777 make
